package com.zhugang.week12;

/**
 * @program algorithms
 * @description: ProductExceptSelf2
 * @author: chanzhugang
 * @create: 2022/09/08 22:23
 */
public class ProductExceptSelf2 {

    /**
     * 238 除自身以外数组的乘积
     *
     * @param nums
     * @return
     */
    public int[] productExceptSelf(int[] nums) {
        // 总乘积除以当前元素
        int n = nums.length;
        int product = 1;
        for (int i = 0; i < n; i++) {
            product *= nums[i];
        }

        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            // 元素不能含有0
            array[i] = product / nums[i];
        }
        return array;
    }
}